package com.skivingcloud.admin.utils;


import org.jasypt.util.text.BasicTextEncryptor;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class JasyptUtil{

	/**
	 * 加密方法
	 * @param salt 盐值
	 * @param targetString 待加密字符串
	 * @return 密文
	 */
	public static String encrypt(String salt, String targetString) {
		BasicTextEncryptor encryptor = new BasicTextEncryptor();
		encryptor.setPassword(salt);
		return encryptor.encrypt(targetString);
	}
	
	/**
	 * 解密方法
	 * @param salt 盐值
	 * @param targetString 待解密字符串
	 * @return 明文
	 */
	public static String decrypt(String salt,String targetString) {
		BasicTextEncryptor encryptor = new BasicTextEncryptor();
		encryptor.setPassword(salt);
		return encryptor.decrypt(targetString);
	}
	
	public static void main(String[] args) {
		String salt = "jqqrIct4MsmrCT6LRiPEWXR1m8NsIqNp";
		String password = "1qaz!QAZ";
		// 进行加密操作
		String encryptString1 = encrypt(salt, password);
		// 进行解密操作
		String decryptString1 = decrypt(salt, encryptString1);
		// 输出明文和密文
		System.out.println("加密后的串： ENC(" + encryptString1 + ")");
		System.out.println("解密后的串：" + decryptString1);
		System.out.println("Basic " + Base64.encode(("skivingcloud:1qaz!QAZ").getBytes(StandardCharsets.ISO_8859_1)));
	}
}
